package android.bignerdranch.xiangmu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends AppCompatActivity implements View.OnClickListener{

    private static final String PREFS_NAME ="android.bignerdranch.xiangmu" ;
    EditText et_username;
    EditText et_password;
    Button btn_log_in;
    Button btn_registered;
    CheckBox cb_remember;
    CheckBox cb_auto;
    MyHelper myHelper;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    int isRemember = 0;
    int auto = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log_in);
        Init();


    }

    void Init()
    {
        et_username = findViewById(R.id.username_input);
        et_password = findViewById(R.id.password_input);
        btn_log_in = findViewById(R.id.btn_Login);
        btn_registered = findViewById(R.id.btn_Registered);
        cb_remember = findViewById(R.id.cb_Remember);
        cb_auto = findViewById(R.id.cb_auto);
        sharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = sharedPreferences.edit();
        btn_log_in.setOnClickListener(this);
        btn_registered.setOnClickListener(this);
        cb_auto.setOnClickListener(this);
        if(sharedPreferences.getInt("isRemember",0) == 1) {
            et_username.setText(sharedPreferences.getString("user_name", null));
            et_password.setText(sharedPreferences.getString("password", null));
        }
        if(sharedPreferences.getInt("auto",0) == 1)
        {
            et_username.setText(sharedPreferences.getString("lasted_user", null));
            et_password.setText(sharedPreferences.getString("lasted_password", null));
            cb_remember.setChecked(true);
            cb_auto.setChecked(true);
            editor.apply();
            btn_log_in.performClick();
        }
        editor.apply();
    }

    @Override
    public void onClick(View v) {
        final int btn_Login = R.id.btn_Login;
        final int btnRegistered = R.id.btn_Registered;
        final int cb_autoed = R.id.cb_auto;
        switch (v.getId()) {
            case btn_Login:{
                String name = sharedPreferences.getString("user_name",null);
                String pass = sharedPreferences.getString("password",null);
                String t_pass = new String();
                myHelper = new MyHelper(this);
                SQLiteDatabase db = myHelper.getWritableDatabase();
                Cursor c = db.query("user", null, "name=?",
                        new String[]{et_username.getText().toString().trim()}, null, null, null);
                while(c.moveToNext()){
                    t_pass = c.getString(c.getColumnIndex("password"));
                }
                c.close();
                if(et_username.getText().toString().equals(name) && et_password.getText().toString().equals(t_pass))
                {
                    Toast.makeText(Login.this,"登录成功",Toast.LENGTH_SHORT).show();
                    if (cb_remember.isChecked()) {
                    isRemember = 1;
                    }
                    else {
                    isRemember = 0;
                    }
                    if(cb_auto.isChecked())
                    {
                        auto = 1;
                        editor.putString("lasted_user", et_username.getText().toString());
                        editor.putString("lasted_password", et_password.getText().toString());
                        cb_remember.isChecked();
                    }
                    Intent intent = new Intent();
                    intent.setClass(Login.this,Main.class);
                    startActivity(intent);
                }
                else{
                    Toast.makeText(Login.this,"登录失败，请检查用户名或者密码是否输入正确",Toast.LENGTH_SHORT).show();
                }
                editor.putInt("auto", auto);
                editor.putInt("isRemember", isRemember);
                editor.apply();

                break;
            }
            case btnRegistered:{
                Intent intent = new Intent();
                intent.setClass(Login.this,Register.class);
                startActivity(intent);
                break;
            }
            case cb_autoed:{
                auto = 1;
                cb_remember.setChecked(true);
                break;
            }
        }

    }
}